import CoreUtil from "../base/CoreUtil";
import IUIWindow from "./IUIWindow";
import * as fgui from "fairygui-cc";
import WindowManager from "./WindowManager";
import ObjectUtil from "../../../Core/Utils/ObjectUtil";
import UICore from "../../../Core/Res/UICore";
import { tween, Tween } from "cc";
import EventMgr from "../../../Core/Event/EventMgr";
import TimerMgr from "../../../Core/Timer/TimerMgr";

export default class AbstractUIWindow implements IUIWindow {
    protected _resList: Array<string>;
    protected _isShowCenter: boolean = true;
    protected _modal: boolean = false;
    protected _isInited: boolean = false;
    protected _isShow: boolean = false;
    protected _touchClose: boolean = false;
    protected _needShowAction: boolean = false;
    protected _autoDispose: boolean = true;
    protected _autoDisposeTime: number = 5000;
    protected _windowData: any;
    protected _lastCloseTime: number;
    protected _loadResCallback: Function;
    protected _needCloseAction: boolean = false;
    protected _isPlayClose: boolean;
    protected _view: fgui.GComponent;
    protected _notClose: boolean;
    public constructor() {
        this._resList = this.getResList();
    }
    public show(modal: boolean, touchClose?: boolean, args?: Array<any>): void {
        this._modal = modal;
        this._touchClose = touchClose;
        this._lastCloseTime = NaN;
        WindowManager.ins.showWindow(this);
        this.onShow.apply(this, args);
    }
    protected onShow(...args: Array<any>): void {
        //由子类实现
    }
    /**
     * 初始化
     */
    public init(): void {
        this.onInit();

        if (this._view == null) {
            console.log("视图为空");
        } else {
            this.onInitView();
        }
        this._isInited = true;
    }
    /**
     * 设置坐标
     */
    public position(x?: number, y?: number): void {
        WindowManager.ins.positionWindow(this as IUIWindow, x, y);
    }
    /**
    * 设置在最顶层
    */
    public bringToFront(): void {
        WindowManager.ins.bringToFront(this as IUIWindow);
    }
    /**
  * 移动窗体
  */
    public moveTo(x: number, y: number): void {
        if (this.view != null) {
            this.view.x = x;
            this.view.y = y;
        }
    }

    /**
     * 播放显示动画
     */
    public playShowAnimation(): void {

    }

    /**
     * 播放关闭动画
     */
    public playCloseAnimation(): void {
        if (this.view && this._needCloseAction) {
            this.view.alpha = 0.8;
            tween(this.view).to(0.1, { scaleX: 1.05, scaleY: 1.05 }).call(() => {
                this.view.alpha = 1;
                this.view.scaleY = 1;
                this.view.scaleX = 1;
                this.playCloseAniEnd();
            }).start();
        } else {
            this.playCloseAniEnd();
        }
    }
    public playCloseAniEnd(): void {
        this._isPlayClose = false;
        this._lastCloseTime = Date.now();
        this.touchClose = true;
        WindowManager.ins.closeWindow(this);
        this._loadResCallback = null;
        if (this._isInited) {
            this.onClose();
        }
        this.dispose();
    }
    protected onClose(): void {
        //由子类重写
    }
    /**
    * 缓动移动窗体
    */
    public tweenMoveTo(x: number, y: number, time: number): void {
    }
    /**
    * 销毁
    */
    public dispose(): void {
        if (this._isInited) {
            this.onDispose();
        }

        // 销毁视图
        if (this._view != null) {
            this._view.dispose();
            this._view = null;
        }
        this._resList = null;
        console.log("销毁窗体：", this);
        ObjectUtil.clear(this);
    }
    protected onDispose(): void {

    }
    /**
    * 加载窗体资源
    */
    public loadRes(callback?: Function): void {
        if (this.hasRes) {
            if (callback != null) {
                callback();
                return;
            }
        }
        this._loadResCallback = callback;
        UICore.loadPackageList(this._resList, this._loadResCallback);
    }
    /**
    * 关闭
    */
    public close(): void {
        EventMgr.targetOff(this);
        this._view.clearClick();
        Tween.stopAllByTarget(this.view);
        this.playCloseAnimation();
    }
    protected onInitView(): void {
        WindowManager.ins.setSkinPart(this.view, this);
        if (this.view.getChild(`btnClose`)) {
            this.view.getChild(`btnClose`).onceClick(() => {
                this.close();
            }, this);
        }
    }

    protected onInit(): void {
        //由子类重写
    }

    /**
     * 视图
     */
    public get view(): fgui.GComponent {
        return this._view;
    }
    protected getResList(): Array<string> {
        return [];
    }
    /**
     * 全资源列表（包括子界面）
     */
    public getAllResList(): Array<string> {
        return this._resList;
    }
    /**
     * 判断是否有资源
     */
    public get hasRes(): boolean {
        if (this._resList == null || this._resList.length == 0) {
            return true;
        }
        return CoreUtil.ins.hasPackageList(this._resList);
    }
    /**
     * 资源列表
     */
    public get resList(): Array<string> {
        return this._resList;
    }
    /**
     * 是否模态
     */
    public get modal(): boolean {
        return this._modal;
    }

    public set modal(value: boolean) {
        this._modal = value;
    }
    public get notClose(): boolean {
        return this._notClose;
    }
    public set notClose(val: boolean) {
        this._notClose = val;
    }
    /**
     * 是否触摸关闭
     */
    public get touchClose(): boolean {
        return this._touchClose;
    }

    public set touchClose(value: boolean) {
        this._touchClose = value;
    }
    /**
    * 是否已初始化
    */
    public get isInited(): boolean {
        return this._isInited;
    }

    public set isInited(value: boolean) {
        this._isInited = value;
    }
    /**
     * 是否已显示
     */
    public get isShow(): boolean {
        return this._isShow;
    }

    public set isShow(value: boolean) {
        this._isShow = value;
    }
    /**
     * 是否居中显示
     */
    public get isShowCenter(): boolean {
        return this._isShowCenter;
    }

    public set isShowCenter(value: boolean) {
        this._isShowCenter = value;
    }
    /**
    * 窗体数据
    */
    public get windowData(): any {
        return this._windowData;
    }

    public set windowData(value: any) {
        this._windowData = value;
    }
    /**
         * 是否显示动画
         */
    public get needShowAction(): boolean {
        return this._needShowAction;
    }

    public set needShowAction(value: boolean) {
        this._needShowAction = value;
    }
    /**
 * 是否自动销毁
 */
    public get autoDispose(): boolean {
        return this._autoDispose;
    }

    public set autoDispose(value: boolean) {
        this._autoDispose = value;
    }

    /**
     * 自动销毁时间
     */
    public get autoDisposeTime(): number {
        return this._autoDisposeTime;
    }

    public set autoDisposeTime(value: number) {
        this._autoDisposeTime = value;
    }
    /**
         * 最后一次显示时间
         */
    public get lastCloseTime(): number {
        return this._lastCloseTime;
    }

    public set lastCloseTime(value: number) {
        this._lastCloseTime = value;
    }
}